package com.jiulan.hearthearing.ui.heart.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.gson.Gson;
import com.jiulan.hearthearing.R;
import com.jiulan.hearthearing.base.BaseActivity;
import com.jiulan.hearthearing.constants.UrlConstants;
import com.jiulan.hearthearing.http.HttpRequest;
import com.jiulan.hearthearing.http.HttpStringCallBack;
import com.jiulan.hearthearing.ui.heart.fragment.ClassifyFragment;
import com.jiulan.hearthearing.ui.heart.fragment.CourseLiveFragment;
import com.jiulan.hearthearing.ui.heart.model.ClassifyModel;
import com.jiulan.hearthearing.widget.NoScrollViewPager;

import java.util.HashMap;

/**
 * 分类
 */
public class ClassifyActivity extends BaseActivity {
    private int from;
    private LayoutInflater inflater;
    private ClassifyModel model;
    private ClassifyPagerAdapter adapter;
    private int currentItem = 0;
    private int scrllViewWidth = 0, scrollViewMiddle = 0;

    private ScrollView scrollView;
    private LinearLayout llClassify;
    private NoScrollViewPager viewpager;

    public static void actionStart(Context context, int from) {
        Intent intent = new Intent(context, ClassifyActivity.class);
        intent.putExtra("from", from);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_classify;
    }

    @Override
    protected void initView() {
        from = getIntent().getIntExtra("from", 1);
        switch (from) {
            case 0:
                setTitleText("电台");
                break;
            case 1:
                setTitleText("直播课程");
                break;
            case 2:
                setTitleText("名师录播");
                break;
            case 3:
                setTitleText("精选文章");
                break;
            case 4:
                setTitleText("全部分类");
                break;
        }
        inflater = LayoutInflater.from(getContext());
        scrollView = findViewById(R.id.scrollView);
        llClassify = findViewById(R.id.llClassify);
        viewpager = findViewById(R.id.viewpager);
        adapter = new ClassifyPagerAdapter(getSupportFragmentManager());
        getData();
    }

    @Override
    protected void initData() {

    }

    private void getData() {
        new HttpRequest(getContext()).doGet(UrlConstants.course_category, "", new HashMap(), ClassifyModel.class, new HttpStringCallBack() {
            @Override
            public void onSuccess(Object result) {
                if (result instanceof ClassifyModel) {
                    model = (ClassifyModel) result;
                    setTab();
                }
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }

    private TextView classifyTextViews[];
    private View views[];

    private void setTab() {
        classifyTextViews = new TextView[model.getData().size()];
        views = new View[model.getData().size()];
        llClassify.removeAllViews();
        for (int i = 0; i < model.getData().size(); i++) {
            View view = inflater.inflate(R.layout.item_classify_group, null);
            view.setId(i);
            view.setOnClickListener(toolsItemListener);
            TextView tvGroupName = view.findViewById(R.id.tvGroupName);
            tvGroupName.setText(model.getData().get(i).getCate_name());
            llClassify.addView(view);
            classifyTextViews[i] = tvGroupName;
            views[i] = view;
        }
        changeTextColor(0);
        viewpager.setAdapter(adapter);
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (viewpager.getCurrentItem() != position) viewpager.setCurrentItem(position);
                if (currentItem != position) {
                    changeTextColor(position);
                    changeTextLocation(position);
                }
                currentItem = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 改变textView的颜色
     *
     * @param id
     */
    private void changeTextColor(int id) {
        for (int i = 0; i < classifyTextViews.length; i++) {
            if (i != id) {
                classifyTextViews[i].setBackgroundColor(getResources().getColor(R.color.white));
                classifyTextViews[i].setTextColor(getResources().getColor(R.color.black_light));
            }
        }
        classifyTextViews[id].setBackgroundColor(getResources().getColor(R.color.common_bg));
        classifyTextViews[id].setTextColor(getResources().getColor(R.color.title_bg));
    }

    /**
     * 改变栏目位置
     *
     * @param clickPosition
     */
    private void changeTextLocation(int clickPosition) {

        int x = (views[clickPosition].getTop() - getScrollViewMiddle() + (getViewheight(views[clickPosition]) / 2));
        scrollView.smoothScrollTo(0, x);
    }

    /**
     * 返回scrollview的中间位置
     *
     * @return
     */
    private int getScrollViewMiddle() {
        if (scrollViewMiddle == 0)
            scrollViewMiddle = getScrollViewheight() / 2;
        return scrollViewMiddle;
    }

    /**
     * 返回ScrollView的宽度
     *
     * @return
     */
    private int getScrollViewheight() {
        if (scrllViewWidth == 0)
            scrllViewWidth = scrollView.getBottom() - scrollView.getTop();
        return scrllViewWidth;
    }

    /**
     * 返回view的宽度
     *
     * @param view
     * @return
     */
    private int getViewheight(View view) {
        return view.getBottom() - view.getTop();
    }

    private View.OnClickListener toolsItemListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            viewpager.setCurrentItem(v.getId());
        }
    };

    class ClassifyPagerAdapter extends FragmentPagerAdapter {

        public ClassifyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        //创建fragment对象并返回
        @Override
        public Fragment getItem(int position) {
            Bundle bundle = new Bundle();
            bundle.putInt("from", from);
            bundle.putInt("id", model.getData().get(position).getId());
            bundle.putString("child", new Gson().toJson(model.getData().get(position).getChild()));
            switch (from) {
//                case 0://公开课FM
//                    CourseRadioFragment radioFragment = new CourseRadioFragment();
//                    radioFragment.setArguments(bundle);
//                    return radioFragment;
                case 1://直播课程
                    CourseLiveFragment liveFragment = new CourseLiveFragment();
                    liveFragment.setArguments(bundle);
                    return liveFragment;
//                case 2://名师录播
//                    CourseLuboFragment luboFragment = new CourseLuboFragment();
//                    luboFragment.setArguments(bundle);
//                    return luboFragment;
//                case 3://精选文章
//                    CourseArticleFragment articleFragment = new CourseArticleFragment();
//                    articleFragment.setArguments(bundle);
//                    return articleFragment;
                case 0://公开课FM
                case 2://名师录播
                case 3://精选文章
                case 4://全部分类
                    ClassifyFragment classifyFragment = new ClassifyFragment();
                    classifyFragment.setArguments(bundle);
                    return classifyFragment;
            }
            return null;
        }

        //返回数量
        @Override
        public int getCount() {
            return model.getData().size();
        }
    }

    @Override
    protected int getTitleBarType() {
        return TITLEBAR_RIGHT_IMG;
    }

}
